package com.codedancer.campus.manager.api.plate;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.plate.ro.PlateAddOrUpdateRo;
import com.codedancer.campus.manager.api.plate.ro.PlatePageSearchRo;
import com.codedancer.campus.manager.service.IPlateService;
import com.codedancer.campus.manager.service.bo.plate.PlateAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.plate.PlatePageSearchBo;
import com.codedancer.campus.manager.service.vo.plate.PlatePageVo;
import com.codedancer.campus.manager.service.vo.plate.PlateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/plate")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "板块Api", description = "板块Api", tags = "板块Api")
public class PlateApi extends BaseApi {

    @Autowired
    private IPlateService plateService;

    @GetMapping("/page")
    @ApiOperation("分页查询板块")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<IPage<PlatePageVo>> page(@ApiParam(value = "板块分页查询Ro", required = false)
        PlatePageSearchRo platePageSearchRo) {
        PlatePageSearchBo platePageSearchBo = BeanUtil.copyProperties(platePageSearchRo,
            PlatePageSearchBo.class);
        return success(plateService.page(platePageSearchBo));
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除板块")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "板块uuid", required = true) String uuid) {
        plateService.delete(uuid);
        return success();
    }

    @PostMapping("/add")
    @ApiOperation("添加板块")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> add(@RequestBody @Valid @ApiParam(value = "板块添加Ro", required = true)
        PlateAddOrUpdateRo plateAddOrUpdateRo) {
        PlateAddOrUpdateBo plateAddOrUpdateBo = BeanUtil.copyProperties(plateAddOrUpdateRo,
            PlateAddOrUpdateBo.class);
        plateService.add(plateAddOrUpdateBo);
        return success();
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改板块")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> update(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "板块uuid", required = true) String uuid,
        @RequestBody @Valid @ApiParam(value = "板块修改Ro", required = true) PlateAddOrUpdateRo plateAddOrUpdateRo) {
        PlateAddOrUpdateBo plateAddOrUpdateBo = BeanUtil.copyProperties(plateAddOrUpdateRo,
            PlateAddOrUpdateBo.class);
        plateAddOrUpdateBo.setUuid(uuid);
        plateService.update(plateAddOrUpdateBo);
        return success();
    }

    @GetMapping("/all")
    @ApiOperation("获取所有板块")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<List<PlateVo>> all(){
        return success(plateService.all());
    }
}
